import 'dart:io';
// permission_handler: ^8.1.1
import 'package:permission_handler/permission_handler.dart';


class PermissionAdapter {
  static Future<bool> canReadStorage({openSetting: false}) async {
    // 请求存储卡权限
    if(Platform.isIOS) return true;
    return await request(Permission.storage, openSetting: openSetting);
  }

  static Future<bool> canOpenCamera({openSetting: false}) async {
    // 请求照相机权限
    return await request(Permission.camera, openSetting: openSetting);
  }

  static Future<bool> request(Permission permission, {openSetting: false}) async {
    bool isShown = await permission.shouldShowRequestRationale;
    // 申请权限
    var permStatus = await permission.request();
    if (permStatus.isGranted) {
      return true;
    } else {
      if (!isShown && openSetting) {
        openAppSettings();  // 打开设置权限
        return false;
      } else {
        return false;
      }
    }
  }
}